package general.constraints;

public class ArrayConstraints implements Constraints{

	boolean[][][][] constraints;

	int domain;
	int n;
	int[] numConflicts;
	int[] numConstraints;

	public ArrayConstraints(byte n, byte domain){
		this.domain = domain;
		this.n = n;
		this.numConflicts = new int[n];
		this.numConstraints = new int[n];
		constraints = new boolean[n][n][][];
		reset();
	}

	@Override
	public boolean hasConstraint(byte var1, byte var2, byte val1, byte val2) {
		return constraints[var1][var2]!=null && constraints[var1][var2][val1][val2];
	}

	@Override
	public void reset() {
		for (int i=0; i<n; i++){
			for (int j=0; j<n; j++){
				constraints[i][j]=null;
			}
			numConflicts[i] = 0;
			numConstraints[i] = 0;
		}
	}


	@Override
	public void addConstraint(byte var1, byte var2, byte val1, byte val2) {
		if (var1!=var2){
			numConflicts[var1]++;
			if (constraints[var1][var2]==null){
				numConstraints[var1]++;
				constraints[var1][var2] = new boolean[domain][domain];
			}
			constraints[var1][var2][val1][val2] = true;
		}

	}

	@Override
	public VariableConstraints getVariableConstraints(byte var) {
		return new ArrayVariableConstraints(constraints[var]);
	}

	@Override
	public int getNumConflicts(byte var) {
		return numConflicts[var];
	}

	@Override
	public int getNumConstraints(byte var) {
		return numConstraints[var];
	}

}
